import Vue from 'vue'
import Vuex from 'vuex'
import { login, productAttr, shopCat, addShopCat, userAddress, logout, orderList } from '@/api/requests.js'
import Storage from '@/Storage'

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    userInfo: '',
    productList: {},
    shopCatList: {},
    userAddress: [],
    orderList: []
  },
  getters: {
    isSelect(state) {
      const list = state.shopCatList.list || []
      // 满足slected为true就返回这个item
      return list.filter(item => item.selcted)
    }

  },
  mutations: {
    LOGIN(state, userInfo) {
      state.userInfo = userInfo
    },
    PRODUCT(state, productList) {
      state.productList = productList
    },
    SHOPCAT(state, data) {
      state.shopCatList = data
    },
    ADDSHOPNUM(state, id) {
      state.shopCatList.list.forEach(item => {
        if (item.id === id) {
          item.num++
        }
      })
    },
    ADDSHOPCAT(state, data) {
      state.shopCatList.list.push(data)
    },
    // 修改购物车全选状态
    CHANGEALLCHECKED(state, status) {
      state.shopCatList.selctedAll = status
      state.shopCatList.list.forEach(item => {
        item.selcted = status
      })
    },
    // 修改商品单选状态
    CHANGECHECKED(state, data) {
      state.shopCatList.list.forEach(item => {
        if (item.id === data.id) {
          item.selcted = data.flag
        }
      })
      const allCheckedStatus = state.shopCatList.list.every(item => {
        let flag = false
        if (item.selcted) {
          flag = true
        }
        return flag
      })
      state.shopCatList.selctedAll = allCheckedStatus
    },
    // 增加商品的数量
    ADDORSUB(state, data) {
      state.shopCatList.list.forEach(item => {
        if (item.id === data.id) {
          if (data.status === 'add') {
            item.num++
            if (item.num > 10) {
              item.num = 10
              alert('商品数量不能大于10')
            }
          } else {
            item.num--
            if (item.num < 1) {
              item.num = 1
              alert('商品数量不能小于1')
            }
          }
        }
      })
    },
    // 输入框失去焦点后修改商品数量
    BLURCHANGENUM(state, { id, value }) {
      state.shopCatList.list.forEach(item => {
        if (item.id === id) {
          state.id = id
          item.num = value
        }
      })
    },
    // 删除商品
    REMOVEPRODCUT(state, index) {
      state.shopCatList.list.splice(index, 1)
    },
    // 用户地址
    ADDRESS(state, data) {
      state.userAddress = data
    },
    // 修改用户地址选中状态
    CHANGEADDRESSCHECKED(state, id) {
      state.userAddress.forEach(item => {
        item.ischecked = false
        if (item.id === id) {
          item.ischecked = true
        }
      })
    },
    // 退出登入
    LOGOUT(state) {
      state.userInfo = ''
      state.shopCatList = ''
    },
    // 刷新后获取本地中的数据并且存储到state中
    LOADING(state, data) {
      // 利用对象的数据赋值 将data中的数据拷贝到state中
      Object.assign(state, data)
    },
    ORDERLIST(state, data) {
      state.orderList = data
    },
    // 添加订单数据
    ADDORDERLIST(state, data) {
      state.orderList.unshift(data)
    }
  },
  actions: {
    async login({ commit }, { username, password }) {
      const res = await login(username, password)
      if (res) {
        commit('LOGIN', res.data)
        Storage.setItem('token', res.data.token)
        return true
      } else {
        return false
      }
    },
    async logout({ commit }) {
      const res = await logout()
      if (res.code === 200) {
        commit('LOGOUT')
        Storage.clear('token')
        return true
      }
    },
    async getProductList({ commit }, id) {
      const res = await productAttr(id)
      commit('PRODUCT', res.data)
    },
    // 获取购物车数据
    async getShopCat({ commit, state }) {
      const arr = Object.keys(state.shopCatList)
      if (arr.length === 0) {
        const res = await shopCat()
        commit('SHOPCAT', res.data)
      }
    },
    // 添加商品到购物车中
    async getAddShopCat({ commit, state }, id) {
      const res = await addShopCat(id)
      res.data.selcted = true
      const isAdd = state.shopCatList.list.some(item => {
        let flag = false
        if (item.id === id) {
          flag = true
        }
        return flag
      })
      if (isAdd) {
        commit('ADDSHOPNUM', id)
      } else {
        commit('ADDSHOPCAT', res.data)
      }
      return true
    },
    // 获取用户地址
    async getUserAddress({ commit }, state) {
      const res = await userAddress()
      commit('ADDRESS', res.data)
      return res.data
    },
    // 获取订单数据
    async getOrderList({ commit }) {
      const res = await orderList()
      commit('ORDERLIST', res.data)
    }
  },
  modules: {
  }
})
